﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace produccion.clases
{
    class CLSENCDEVP:ObservableObject
    {

        public int insertar() {
            try {
                NRODEV_EDV = QueryNroDev();
                string query = "insert into encdevp (NRODEV_EDV,FECDEV_EDV,CODCLI_EDV,DOCREF_EDV,OBSERV_EDV) values ('"+NRODEV_EDV+"','"+FECDEV_EDV.ToShortDateString()+"','"+CODCLI_EDV+"','"+DOCREF_EDV+"','"+OBSERV_EDV+"')";
                return mod.comando(query);
            }
            catch (Exception ex) { throw ex; }
        }
        
        public int guardar(CLSENCDEVP enc, List<CLSDETDEVP> lis)
        {
            try
            {
                MDOgeneral.transaccion = MDOgeneral.conexion.BeginTransaction();
                enc.insertar();

                Boolean sw = true;
                string consecutivo = "0";

                foreach (CLSDETDEVP r in lis)
                {
                    r.NRODEV_DDV = enc.NRODEV_EDV;
                    
                    CLSENCORDP encord = new CLSENCORDP();
                    encord.TIPORD_EOP = r.TIPO_DDV;

                 
                    if (sw) {

                        consecutivo = encord.Consecutivo("D");
                        sw = false;
                    }
                    else{
                    
                       long aux= System.Convert.ToInt64(consecutivo.Substring(1, 5));
                       aux=aux+1;
                       consecutivo = "D" + aux.ToString("00000");
                    }


                    encord.NROORD_EOP = consecutivo;
                    encord.FECORD_EOP = DateTime.Today;
                    encord.CODCLI_EOP = enc.CODCLI_EDV;
                    encord.NROPED_EOP = 0;
                    encord.CODREF_EOP = r.CODREF_DDV;
                    encord.CODCOL_EOP = r.CODCOL_DDV;
                    encord.CODHOR_EOP = " ";
                    encord.CODCOR1_EOP = " ";
                    encord.PORCOR1_EOP = 0;
                    encord.ESTADO_EOP = r.TIPO_DDV;
                    CLSDETORDP detord = new CLSDETORDP();
                    detord.NROORD_DOP = encord.NROORD_EOP;
                    detord.CODREF_DOP = encord.CODREF_EOP;
                    detord.CODCOL_DOP = encord.CODCOL_EOP;
                    detord.CODTAL_DOP = r.CODTAL_DDV;
                    detord.CANPRG_DOP = r.CANPRO_DDV;
                    detord.CANORD_DOP = r.CANPRO_DDV;
                    encord.insertar();
                    detord.insertar();
                    

                    r.NROORD_DDV = encord.NROORD_EOP;

                    r.insertar();
                }
                MDOgeneral.transaccion.Commit();
                return 1;
            }
            catch (Exception ex) { MDOgeneral.transaccion.Rollback(); throw ex; }
        }

        public string QueryNroDev() {
            try {
                string query = "select coalesce(MAX(TO_NUMBER(NRODEV_EDV)),0)+1 AS MAX_NRODEV_EDV from ENCDEVP";
                DataTable val= mod.consulta(query);
                return val.Rows[0][0].ToString();
            }
            catch (Exception ex) { throw ex; }
        }

        //Consulta ordenes de produccion por orden de dev.
        public DataTable consOrdProxOrdDev() {
            try {

                string query = "select NROORD_DOP,CODREF_DOP,NOMREF_REF,CODCOL_DOP,NOMCOL_COL,CODTAL_DOP,CANORD_DOP,TIPORD_EOP from detdevp ddev inner join detordp dordp   on NROORD_DOP=NROORD_DDV inner join maeref  mref on CODREF_REF=CODREF_DOP inner join maecol mcol on CODCOL_COL=CODCOL_DOP inner join encordp on NROORD_EOP=NROORD_DDV where NRODEV_DDV='"+NRODEV_EDV+"' order by NROORD_DOP ";
                return mod.consulta(query);
            }
            catch (Exception ex) { throw ex; }
        }

        MDOgeneral mod = new MDOgeneral();

        private string _NRODEV_EDV;
        public string NRODEV_EDV{
            get{return _NRODEV_EDV;}
            set{_NRODEV_EDV=value;
                RaisePropertyChangedEvent("NRODEV_EDV");
            }
        }

        private DateTime _FECDEV_EDV=DateTime.Today;
        public DateTime FECDEV_EDV{
            get{return _FECDEV_EDV;}
            set{_FECDEV_EDV=value;
            RaisePropertyChangedEvent("FECDEV_EDV");
            }
        }
        
        private string _CODCLI_EDV;
        public string CODCLI_EDV{
            get{return _CODCLI_EDV;}
            set{
                _CODCLI_EDV=value;
                RaisePropertyChangedEvent("CODCLI_EDV");
            }
        }

        private string _DOCREF_EDV;
        public string DOCREF_EDV{
            get{return _DOCREF_EDV;}
            set{_DOCREF_EDV=value;
            RaisePropertyChangedEvent("DOCREF_EDV");
            }
        }

        private string _OBSERV_EDV;
        public string OBSERV_EDV {
            get { return _OBSERV_EDV; }
            set { _OBSERV_EDV = value;
            RaisePropertyChangedEvent("OBSERV_EDV");
            }
        }
 
    }
}
